package org.display.graphic
{
	import flash.display.Sprite;
	import flash.events.*;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.*;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;
	
	import mx.flash.UIMovieClip;
	import mx.managers.*;
	
	import org.abcFrame.core.*;
	import org.data.Global;
	import org.utils.*;
	
	import spark.primitives.Path;
	

	public class ShapeBase extends AbcView
	{
		protected var linecolor:uint=0x000000
		protected var linethick:int=1
		protected var linealpha:int=1
			
		protected var fillalpha:Number=0.7
		
		
		protected var hitRec:HitRect
		public var txtField:TextField
		public var square:Square
		
		
		public var rots:Array=[]
		
		public var _roots:Array=[]
		

		public function ShapeBase()
		{
			createMenuItems(["重命名","添加事件","复制","最上层","最下层","删除事件","删除"])

			square=new Square()
			
			this.addEventListener(MouseEvent.MOUSE_DOWN,mouseEvent)
			this.addEventListener(MouseEvent.RIGHT_MOUSE_DOWN,mouseEvent)
			this.addEventListener(MouseEvent.DOUBLE_CLICK,mouseEvent)
			this.addEventListener(MouseEvent.ROLL_OVER,mouseEvent)
			this.addEventListener(MouseEvent.ROLL_OUT,mouseEvent)
	
			this.doubleClickEnabled=true
				
			txtField=new TextField()
			txtField.type=TextFieldType.INPUT
			txtField.border=true
			txtField.width=100
			txtField.height=20
			txtField.background=true
			txtField.selectable=true
			
			txtField.text="abc"+Global.index
			Global.index++
				
			hitRec=new HitRect(this)

			addChild(hitRec)
					
			this.addEventListener(Event.REMOVED_FROM_STAGE,delEvent)
	
		}
		
		public function setName(value:String){
		
			txtField.text=value
		
		}
		
		public function redraw(w:Number,h:Number){
			
		
		}
		
		function delEvent(e:Event){
			
			stage.removeEventListener(MouseEvent.MOUSE_DOWN,mouseEvent)
			stage.removeEventListener(MouseEvent.MOUSE_UP,mouseEvent)	
			
		}
		
	   protected function menuselectEvent(e:ContextMenuEvent){
			
			switch(ContextMenuItem(e.currentTarget).caption){
				
				case "重命名":
					this.dispatchEvent(new MouseEvent(MouseEvent.DOUBLE_CLICK))
					break
				
				case "复制":
					var symbol=clone()
					symbol.x=x+50
					symbol.y=y
					parent.addChild(symbol)
					break
				
				case "添加事件":

					addChild(square)
					
					break
				
				case "删除事件":
					
					if(this.contains(square))removeChild(square)

					break
				
				case "删除":
					ElementControl.cleanObj(this)
					break
				
				case "最下层":
					parent.setChildIndex(this,1)
					break
				
				case "最上层":
					parent.addChild(this)
					break				
				
				case "显示事件流":
					
					if(Global.nowShapeBase!=null)delSquare(Iv( Global.nowShapeBase.parent))
					
					showSquare(Iv(this.parent))
					
				    Global.nowShapeBase=this
					
					break
				
				case "删除事件流":
					
					delSquare(Iv(this.parent))
					
					break	
				
			}
			
		}
	   
	   
	   
	  protected function showSquare(obj:Iv){
		   
		   if(obj==null){return;}
		  
		   ElementControl.cleanObjByType(ClickSquare,obj)
		   
		   var square:ClickSquare=new ClickSquare(obj,this)
		   
		   obj.addChild(square)
		   
		   
		   square.x=obj.getBounds(obj.parent).width
		   square.y=obj.getBounds(obj.parent).height
			   
		   if(obj.contains(this)){
		   
			   square.x=this.x
			   square.y=this.y
		   
		   }
   
		  if(obj._roots.length==0){return;}  
			   
		  for(var i=0;i<obj._roots.length;i++){ 
		      if(obj._roots[i] is Iv){
			     showSquare(obj._roots[i]) 
		      }
		  }

	   }
	  
	  
	  protected function delSquare(obj:Iv){
		  
		  if(obj==null){return;}

		  ElementControl.cleanObjByType(ClickSquare,obj)
		  
		  if(obj._roots.length==0){return;}  
		  
		  for(var i=0;i<obj._roots.length;i++){ 
			  if(obj._roots[i] is Iv){
				  delSquare(obj._roots[i]) 
			  }
		  }
		  
	  }
		
		
		protected function mouseEvent(e:MouseEvent){
			
		    switch(e.type){
				
			case MouseEvent.ROLL_OVER:
					
					hitRec.alpha=1

					break
				
			case MouseEvent.ROLL_OUT:
				
					hitRec.alpha=0
					
					break
				
			case MouseEvent.DOUBLE_CLICK:

				addChild(txtField)
				try{
				stage.focus=txtField
				txtField.selectable=true	
				txtField.setSelection(0,txtField.text.length) 
			    }catch(e:Error){trace(e)} 
				this.removeEventListener(MouseEvent.MOUSE_DOWN,mouseEvent)
				stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseEvent)
				stage.removeEventListener(MouseEvent.MOUSE_UP,mouseEvent)
				break
				
			case MouseEvent.RIGHT_MOUSE_DOWN:

				
					
				break

			case MouseEvent.MOUSE_DOWN:
				
				switch(e.currentTarget){
				 
				case this:

				if(hitRec.hitTestPoint(stage.mouseX,stage.mouseY,true)){return;}

				this.startDrag(false,new Rectangle(0,0,Global.sceneSize,Global.sceneSize))
				
				stage.addEventListener(MouseEvent.MOUSE_UP,mouseEvent)
				stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseEvent)
				break
			    
				case stage:
					
				if(!this.hitTestPoint(stage.mouseX,stage.mouseY,true)){
					
					this.addEventListener(MouseEvent.MOUSE_DOWN,mouseEvent)
					stage.removeEventListener(MouseEvent.MOUSE_DOWN,mouseEvent)
					
					setTxtField()
					
				}
				
				break
				}
				
				break
			
			case MouseEvent.MOUSE_MOVE:
				
				
				for(var i=0;i<rots.length;i++){
				
					rots[i].redraw()

				}
				
				break
			

			case MouseEvent.MOUSE_UP:
				
				this.stopDrag()
				stage.removeEventListener(MouseEvent.MOUSE_UP,mouseEvent)
				stage.removeEventListener(MouseEvent.MOUSE_MOVE,mouseEvent)
				break
			
			}
			

		}
		
		public function createMenuItems(items:Array){

			var myContextMenu:ContextMenu = new ContextMenu();
			myContextMenu.hideBuiltInItems();
			
			for(var i=0;i<items.length;i++){
				
				createItem(items[i])
				
			}
			
			this.contextMenu = myContextMenu;
			
			function createItem(vlaue:String){
				
				var item:ContextMenuItem = new ContextMenuItem(vlaue);
				item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT ,menuselectEvent)
				myContextMenu.customItems.push(item);
				
			}
		
		}
		
		public function showTxtField(){
			
			this.dispatchEvent(new MouseEvent(MouseEvent.DOUBLE_CLICK))

		}
		
		public function setTxtField(){this.removeChild(txtField)}
		
		public function getEvent():String{
		
		    if(this.contains(square)==false){return ""}
			
			var arr:Array=Global.lockPath.split("\\")
				
			arr.shift()
			arr[1]="commands"
		    return arr.join(".")+"."+txtField.text+"Event"
			
		}
		
		//获取事件路径
		public function getEventPath(path:String):String{
			

			path=path.replace(/\//gis,"\\")
			path=path.replace(/.*?(\\xml\\)/gis,"")
				
			var arr:Array=path.split("\\")
			arr.pop()
				
			arr[1]="commands"
				
			return arr.join(".")+"."+txtField.text+"Event"
			
		}
		
		public function getName():String{
		
			if(txtField.text==""){return ""}
			return txtField.text+"_"+getQualifiedClassName(this).split("::")[1].toLowerCase()

		}
		
		protected function clone():ShapeBase{
		
			var copy:Class=getDefinitionByName(getQualifiedClassName(this).replace(/::/gis,".")) as Class
			var symbol:ShapeBase=new copy() as ShapeBase
			var rec:Rectangle=getBounds(parent)
			symbol.redraw(rec.width-2,rec.height-2)
		    return symbol
		
		}
		
		
		
	}
}